﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using cndp.Model;

namespace cndp.View
{
    public partial class FormAddDiverToEvent : Form
    {
        private int _rowOfRegisteredDiver = -1;
        private int _rowOfSignedDiver = -1;
        public BindingList<Diver> DiverRegister = new BindingList<Diver>();
        public BindingList<DiverWithJumpCodes> DiverWithJumpCodes = new BindingList<DiverWithJumpCodes>();
        public string EventBracket;
        public bool EventCoupleBool;
        public string EventFinishTime;
        public string EventGender;
        public int EventId;
        public double EventJumpHeight;
        public int EventRounds;
        public string EventStartTime;
        public JumpAppendix JumpAppendixList = new JumpAppendix();
        public double JumpHeight;
        public BindingList<Diver> SignedToEvent = new BindingList<Diver>();

        public FormAddDiverToEvent()
        {
            InitializeComponent();
        }

        private void FormAddDiverToEvent_Load(object sender, EventArgs e)
        {
            KeyPreview = true;
            dataGridViewEventInformation[0, 0].Value = EventId;
            dataGridViewEventInformation[1, 0].Value = EventCoupleBool;
            dataGridViewEventInformation[2, 0].Value = EventGender;
            dataGridViewEventInformation[3, 0].Value = EventBracket;
            dataGridViewEventInformation[4, 0].Value = EventJumpHeight;
            dataGridViewEventInformation[5, 0].Value = EventRounds;
            dataGridViewEventInformation[6, 0].Value = EventStartTime;
            dataGridViewEventInformation[7, 0].Value = EventFinishTime;
            dataGridViewRegisteredToContest.DataSource = DiverRegister;
            dataGridViewSignedToEvent.DataSource = SignedToEvent;
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            label6.Visible = false;

            if (dataGridViewRegisteredToContest.Rows.Count != 0)
            {
                _rowOfRegisteredDiver = 0;
                dataGridViewRegisteredToContest.Rows[_rowOfRegisteredDiver].Selected = true;
            }

            if (dataGridViewSignedToEvent.Rows.Count != 0)
            {
                _rowOfSignedDiver = 0;
                dataGridViewSignedToEvent.Rows[_rowOfSignedDiver].Selected = true;
            }
        }

        private void buttonAddDiverToEvent_Click(object sender, EventArgs e)
        {
            if (dataGridViewRegisteredToContest.RowCount != 0)
            {
                if (_rowOfRegisteredDiver != -1)
                {
                    var diverFound =
                        SignedToEvent.SingleOrDefault(
                            d => d.PersonalCodeNumber == DiverRegister[_rowOfRegisteredDiver].PersonalCodeNumber);
                    if (diverFound == null)
                    {
                        SignedToEvent.Add(DiverRegister[_rowOfRegisteredDiver]);

                        var diverFound2 =
                            DiverWithJumpCodes.SingleOrDefault(
                                d2 =>
                                    d2.Diver.PersonalCodeNumber ==
                                    DiverRegister[_rowOfRegisteredDiver].PersonalCodeNumber);
                        if (diverFound2 == null)
                        {
                            DiverWithJumpCodes.Add(new Model.DiverWithJumpCodes(DiverRegister[_rowOfRegisteredDiver].Name, DiverRegister[_rowOfRegisteredDiver].Gender, DiverRegister[_rowOfRegisteredDiver].PersonalCodeNumber, DiverRegister[_rowOfRegisteredDiver].Nationality, DiverRegister[_rowOfRegisteredDiver].Club));
                        }                        
                    }
                }
            }
            dataGridViewSignedToEvent.Refresh();
        }

        private void buttonRemoveDiverFromEvent_Click(object sender, EventArgs e)
        {
            if (dataGridViewSignedToEvent.RowCount != 0)
            {
                if (_rowOfSignedDiver != -1)
                {
                    dataGridViewSignedToEvent.Rows.RemoveAt(_rowOfSignedDiver);

                    DiverWithJumpCodes.RemoveAt(_rowOfSignedDiver);
                }
            }
            if (_rowOfSignedDiver == (dataGridViewSignedToEvent.Rows.Count))
            {
                _rowOfSignedDiver--;
            }
        }

        private void buttonSaveChanges_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void labelEvent_Click(object sender, EventArgs e)
        {
        }

        private void dataGridViewRegisteredToContest_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _rowOfRegisteredDiver = e.RowIndex;
        }

        private void dataGridViewSignedToEvent_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _rowOfSignedDiver = e.RowIndex;
        }

        private void buttonEditJumpCode_Click(object sender, EventArgs e)
        {
            if (dataGridViewSignedToEvent.RowCount != 0)
            {
                if (_rowOfSignedDiver != -1)
                {
                    var formSetJumpCode = new FormSetJumpcodes();
                    formSetJumpCode.TotalAmountOfRounds = EventRounds;
                    formSetJumpCode.CurrentRound = 1;
                    formSetJumpCode.labelName.Text = SignedToEvent[_rowOfSignedDiver].Name;
                    formSetJumpCode.labelPersonalCodeNumber.Text = SignedToEvent[_rowOfSignedDiver].PersonalCodeNumber;
                    formSetJumpCode.JumpAppendixList = JumpAppendixList;
                    formSetJumpCode.JumpHeight = JumpHeight;
                    var diverFound =
                        DiverWithJumpCodes.SingleOrDefault(
                            d => d.Diver.PersonalCodeNumber == SignedToEvent[_rowOfSignedDiver].PersonalCodeNumber);
                    if (diverFound != null)
                    {
                        formSetJumpCode.ListOfJumpCodesAndStyles = diverFound.JumpCodesandStyle;
                    }


                    formSetJumpCode.ShowDialog();
                }
            }
        }

        private void FormAddDiverToEvent_MouseDown(object sender, MouseEventArgs e)
        {
        }

        private void FormAddDiverToEvent_KeyDown(object sender, KeyEventArgs e)
        {
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D1)
                buttonAddDiverToEvent.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D2)
                buttonRemoveDiverFromEvent.PerformClick();
            if ((_rowOfSignedDiver != -1) && (dataGridViewSignedToEvent.Rows.Count != 0))
            {
                dataGridViewSignedToEvent.Rows[_rowOfSignedDiver].Selected = true;
            }

            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D3)
                buttonEditJumpCode.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D4)
                buttonDone.PerformClick();
            if (e.KeyCode == Keys.Escape)
                DialogResult = DialogResult.Cancel;
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Tab)
            {
                label1.Visible = !label1.Visible;
                label2.Visible = !label2.Visible;
                label3.Visible = !label3.Visible;
                label4.Visible = !label4.Visible;
                label5.Visible = !label5.Visible;
                label6.Visible = !label6.Visible;
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Down)
            {
                if (_rowOfRegisteredDiver != (dataGridViewRegisteredToContest.Rows.Count - 1))
                {
                    _rowOfRegisteredDiver++;
                    foreach (DataGridViewRow row in dataGridViewRegisteredToContest.Rows)
                    {
                        row.Selected = false;
                    }
                    dataGridViewRegisteredToContest_CellClick(dataGridViewRegisteredToContest,
                        new DataGridViewCellEventArgs(0, _rowOfRegisteredDiver));
                    dataGridViewRegisteredToContest.Rows[_rowOfRegisteredDiver].Selected = true;
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Up)
            {
                if (_rowOfRegisteredDiver != 0)
                {
                    _rowOfRegisteredDiver--;
                    foreach (DataGridViewRow row in dataGridViewRegisteredToContest.Rows)
                    {
                        row.Selected = false;
                    }

                    dataGridViewRegisteredToContest_CellClick(dataGridViewRegisteredToContest,
                        new DataGridViewCellEventArgs(0, _rowOfRegisteredDiver));
                    dataGridViewRegisteredToContest.Rows[_rowOfRegisteredDiver].Selected = true;
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Left)
            {
                if (_rowOfSignedDiver != 0)
                {
                    _rowOfSignedDiver--;
                    foreach (DataGridViewRow row in dataGridViewSignedToEvent.Rows)
                    {
                        row.Selected = false;
                    }
                    dataGridViewSignedToEvent_CellClick(dataGridViewSignedToEvent,
                        new DataGridViewCellEventArgs(0, _rowOfSignedDiver));
                    dataGridViewSignedToEvent.Rows[_rowOfSignedDiver].Selected = true;
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Right)
            {
                if (_rowOfSignedDiver != (dataGridViewSignedToEvent.Rows.Count - 1))
                {
                    _rowOfSignedDiver++;
                    foreach (DataGridViewRow row in dataGridViewSignedToEvent.Rows)
                    {
                        row.Selected = false;
                    }
                    dataGridViewSignedToEvent_CellClick(dataGridViewSignedToEvent,
                        new DataGridViewCellEventArgs(0, _rowOfSignedDiver));

                    dataGridViewSignedToEvent.Rows[_rowOfSignedDiver].Selected = true;
                }
            }
        }
    }
}